package com.ljt.yupictureddd.interfaces.controller;

import com.ljt.yupictureddd.infrastracture.annotation.AuthCheck;
import com.ljt.yupictureddd.infrastracture.common.BaseResponse;
import com.ljt.yupictureddd.infrastracture.common.DeleteRequest;
import com.ljt.yupictureddd.infrastracture.common.ResultUtils;
import com.ljt.yupictureddd.domain.user.constant.UserConstant;
import com.ljt.yupictureddd.infrastracture.exception.ErrorCode;
import com.ljt.yupictureddd.infrastracture.exception.ThrowUtils;
import com.ljt.yupictureddd.domain.user.entity.User;
import com.ljt.yupictureddd.interfaces.vo.user.LoginUserVO;
import com.ljt.yupictureddd.interfaces.vo.user.UserVO;
import com.ljt.yupictureddd.application.service.UserApplicationService;
import com.ljt.yupictureddd.interfaces.dto.user.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserApplicationService userApplicationService;


    /**
     * 用户注册
     *
     * @param request
     * @return
     */
    @AuthCheck(mustRole = "admin")
    @PostMapping("/register")
    public BaseResponse register(@RequestBody UserRegisterRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        long result = userApplicationService.userRegister(request);
        return ResultUtils.success(result);
    }

    /**
     * 用户登陆
     *
     * @param userLoginRequest
     * @return
     */
    @PostMapping("/login")
    public BaseResponse login(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userLoginRequest == null, ErrorCode.PARAMS_ERROR);
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        LoginUserVO loginUserVO = userApplicationService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(loginUserVO);
    }

    /**
     * 获取当前用户
     * @param request
     * @return 脱敏后的用户信息
     */
    @GetMapping("/get/login")
    public BaseResponse getLoginUser(HttpServletRequest request){
        return ResultUtils.success(userApplicationService.getLoginUser(request));
    }

    /**
     * 用户退出系统
     * @param request
     * @return
     */
    @GetMapping("/logout")
    public BaseResponse logout(HttpServletRequest request){
        return ResultUtils.success(userApplicationService.userLogout(request));
    }


    /**
     * 新增用户
     * @param userAddRequest
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse addUser(@RequestBody UserAddRequest userAddRequest){
        return userApplicationService.addUser(userAddRequest);

    }

    /**
     * 根据 id 获取用户（仅管理员）
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<User> getUserById(long id) {
        return userApplicationService.getUserById(id);
    }

    /**
     * 根据 id 获取包装类
     */
    @GetMapping("/get/vo")
    public BaseResponse<UserVO> getUserVOById(long id) {
        BaseResponse<User> response = getUserById(id);
        User user = response.getData();
        return ResultUtils.success(userApplicationService.getUserVO(user));
    }

    /**
     * 删除用户
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest) {
        return userApplicationService.deleteUser(deleteRequest);
    }

    /**
     * 更新用户
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest) {
        return userApplicationService.updateUser(userUpdateRequest);
    }

    /**
     * 分页
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @PostMapping("/list/page/vo")
    public BaseResponse listUserVOByPage(@RequestBody UserQueryRequest userQueryRequest){
        return userApplicationService.listUserVOByPage(userQueryRequest);
    }
}
